package com.yyx.app1.My_function.UI;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.UiThread;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.yyx.app1.Data.BOSSInfo;
import com.yyx.app1.Data.WebOperation;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.Interface.Trancaction;
import com.yyx.app1.MedicationAssistant_function.UI.MedicationAssistant_Activity_;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.tool.JsonObjectPostRequest;
import com.yyx.app1.tool.MySharedPreferences;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by yuexing on 2015/9/16.
 */
@EActivity(R.layout.activity_login)
public class Login_Activity extends Activity {
    private boolean IsBack;//是否跳转还是返回

    @ViewById
    EditText login_username,login_password;

    @ViewById
    TextView login_forget;

    @AfterViews
    public void init()
    {
        IsBack = getIntent().getBooleanExtra("isback", false);
        Log.i("正常","状态登录="+(IsBack?"返回":"跳转"));
    }

    @Click
    public void login_login()
    {
        try {
            MySharedPreferences.clear(this);
            login();
        }
        catch (Exception e)
        {
            Log.e("异常1511011116", e.toString());
        }
    }

    @Click
    public void login_register()
    {
        startActivity(new Intent(Login_Activity.this, Register_Activity_.class));
    }

    @Click
    public void login_forget()
    {
        startActivity(new Intent(this, ForgetPassword_Acitivity_.class));
    }

    @Click
    public void home()
    {
        finish();
        if (((MyApp)getApplication()).getBossInfo()==null)
        {
            startActivity(new Intent(Login_Activity.this, MedicationAssistant_Activity_.class));
        }
    }

    @Background
    public void login()
    {
        String phone = login_username.getText().toString().trim();
        String passwordstr = login_password.getText().toString().trim();
        if (phone.isEmpty() || passwordstr.isEmpty())
        {
            return;
        }
        Trancaction trancaction = new Trancaction() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i("正常","response="+response.toString());
                try {
                    boolean is = false;
                    int statusCode = response.getInt("statusCode");
                    if (statusCode == 200)
                    {
                        is=true;
                    }
                    transaction(is,response);
                }
                catch (Exception e)
                {
                    Log.e("异常1511071421",e.toString());
                }
            }

            @Override
            public Request.Priority getPriority() {
                return Request.Priority.HIGH;
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Login_Activity.this, "网络异常!", Toast.LENGTH_SHORT).show();
            }
        };
        HashMap<String,String> map = new HashMap<>();
        map.put("mobile",phone);
        map.put("password", passwordstr);
        JsonObjectPostRequest.HttpPost(this, map, "appLoginA", trancaction);
    }

    @UiThread
    public void transaction(boolean login,JSONObject response) throws Exception {
        String message = response.getString("message");
        if (login)
        {
            ((MyApp)this.getApplication()).setBossInfo(BOSSInfo.setBossInfo(response));
            Toast.makeText(Login_Activity.this, message, Toast.LENGTH_SHORT).show();
            if (IsBack)
            {
                Login_Activity.this.finish();
            }
            else
            {
                startActivity(new Intent(Login_Activity.this, My_Acitivity_.class));
            }
        }
        else
        {
            Toast.makeText(Login_Activity.this, message, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            home();
        }
        return true;
    }


}
